(function (window) {
  //设置ajax请求完成后运行的函数,
  // $.ajaxSetup({ 
  //     complete:function(jqxhr){
  //       // console.log(jqxhr)
  //         if("REDIRECT" == jqxhr.getResponseHeader("REDIRECT")){ //若HEADER中含有REDIRECT说明后端想重定向，
  //             var win = window;
  //             while(win != win.top){
  //                 win = win.top;
  //             }
  //             win.location.href = '/html/login.html';//将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
  //         }
  //     }
  // });
  $(function(){
    window.__ = {
      // path: '/wm/manager',
      path: '/manager',
      gobalB64: false,    
      isArray:function(o){
        return Object.prototype.toString.call(o)=='[object Array]';
      },
      link:function(){
        if(location.href.split('#').length<=1)return null;
        return location.href.split('#')[1].split('?')[0];
      },
      genderMap:function(num){
        var gender='未知';
        switch (num) {
          case "0":
            gender='--';
            break;
          case "1":
            gender='男';
            break;
          case "2":
            gender='女'
            break;
          default:
            console.log('gender:'+num);
        }
        return gender;
      },
      obj2query:function(obj){
        var queryStr=``;
        for (const key in obj) {
          queryStr+=`${key}=${obj[key]}&`
        }
        return queryStr.substring(0,queryStr.length-1);
      },
      query:function(){
        var link=location.href.split('?')[1];
        var query={}        
        if(!!link){
          var queryStr=link.split('&');
          queryStr.forEach(item => {
            var tmp=item.split('=');
            query[tmp[0]]=decodeURI(tmp[1]);
          });
        }
        return query;
      },
      getDataObj:function(){
        var b64 = $.base64
        var data = localStorage.getItem('__data');
        if (!!data) {
          if (b64 && __.gobalB64) {
            data = JSON.parse(b64.decode(data))
          } else {
            data = JSON.parse(data)
          }
        } else {
          data = {}
        }
        return data;
      },
      del:function(key){
        var data=this.getDataObj();
        var b64 = $.base64;      
        delete data[key];
        if (b64 && __.gobalB64) {
          localStorage.setItem('__data', b64.encode(JSON.stringify(data)));
        } else {
          localStorage.setItem('__data', JSON.stringify(data));
        }
      },
      data: function (key, val) {
        var b64 = $.base64
        if (__.gobalB64 && !b64) {
          console.error('CH:你开启了全局数据base64转换，这项功能基于jQueryBase64插件，而我们无法找到该插件')
          console.error('EN:$.base64 is no define')
        }
        var data=this.getDataObj();
        
        if (!!!val&&(typeof key)!='object') {
          return data[key]
        } else {
          if((typeof key)=='object'){
            if(!__.isArray(key)){
              for (const k in key) {
                data[k]=key[k]
              }
            }else{
              for (let index = 0; index < key.length; index++) {
                data[index]=key[index]              
              }
            }
          }else{
            data[key] = val;
          }
          if (b64 && __.gobalB64) {
            localStorage.setItem('__data', b64.encode(JSON.stringify(data)));
          } else {
            localStorage.setItem('__data', JSON.stringify(data));
          }
          return this;
        }
      }
    };
    $(window).trigger('inited')
  })
  // window.addEventListener('load',)
})(window)